package wawi.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import wawi.db.DbConnector;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LieferantenListe extends ActionSupport
{
	private ArrayList<ArrayList<String>> aaData = new ArrayList<ArrayList<String>>();

	public ArrayList<ArrayList<String>> getAaData() {
		return aaData;
	}

	public void setAaData(ArrayList<ArrayList<String>> aaData) {
		this.aaData = aaData;
	}
	
	public String execute()
	{
		List<Lieferant> list = DbConnector.getInstance().getLieferantenList();
		
		for (Lieferant tmp : list)
		{
			ArrayList<String> row = new ArrayList<String>();
			row.add(tmp.getFirma());
			row.add(tmp.getAdresse());
			row.add(tmp.getTelefon());
			row.add(tmp.getMail());
			row.add(String.format("<a href=\"Lieferant_bearbeiten.action?recid=%d\">Bearbeiten</a>", tmp.getRecid()));
			
			aaData.add(row);
		}
		
		return ActionSupport.SUCCESS;
	}
	
	public String lieferungLieferanten()
	{
		List<Lieferant> list = DbConnector.getInstance().getLieferantenList();
	
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get( ServletActionContext.HTTP_REQUEST);
		int lager = Integer.parseInt(request.getParameter("lager"));
		
		for (Lieferant tmp : list)
		{
			ArrayList<String> row = new ArrayList<String>();
			row.add(tmp.getFirma());
			row.add(tmp.getAdresse());
			row.add(String.format("<a href=\"Lieferungen_offen.action?lieferant=%d&lager=%d\">W&auml;hlen</a>", tmp.getRecid(), lager));
			
			aaData.add(row);
		}
		
		return ActionSupport.SUCCESS;
	}
}
